package com.xh.one2one.lesson.controller;

import com.xh.bussiness.tiku.bean.dto.QuestionDto;
import com.xh.core.util.StringUtils;
import com.xh.one2one.lesson.bean.dto.LessonTimesDto;
import com.xh.one2one.lesson.service.ILessonTimesService;
import com.xh.one2one.lesson.model.LessonTimes;
import com.xh.one2one.lesson.bean.request.LessonTimesRequest;
import com.xh.core.bean.XhExportHeaderBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.xh.core.controller.BaseController;

/**
 * <p>
 * 一对一备课--》课次 前端控制器
 * </p>
 *
 * @author TW
 * @since 2021-04-09
 */
@RestController
@RequestMapping("/admin/lessonTimes")
public class LessonTimesController extends BaseController {

    @Autowired
    private ILessonTimesService lessonTimesService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public AjaxResult queryPage(XhPage page, LessonTimesRequest bean){
        page=lessonTimesService.queryLessonTimesPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",page);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(LessonTimesRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonTimesService.queryLessonTimes(bean));
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryContent")
    public AjaxResult queryContent(LessonTimesRequest bean){
        if(StringUtils.isEmpty(bean.getLessonId())){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误!");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonTimesService.queryAllLessonTimes(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(LessonTimesRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=lessonTimesService.exportAll(request,exportHeaderBeans,bean,"queryLessonTimesPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param lessonTimes 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated LessonTimes lessonTimes){
        if(lessonTimes.getId()==null){
            lessonTimesService.insert(lessonTimes);
        }else{
            lessonTimesService.update(lessonTimes);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 保存
     * @param request 实体对象
     * @return
     */
    @PostMapping(value="/save")
    @Transactional
    public AjaxResult save(@Validated @RequestBody List<LessonTimesRequest> request){

        lessonTimesService.save(request);

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 保存
     * @param request 实体对象
     * @return
     */
    @PostMapping(value="/batchSaveForStudent")
    @Transactional
    public AjaxResult batchSaveForStudent(@Validated @RequestBody List<LessonTimesRequest> request){
        if(CollectionUtils.isEmpty(request)){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误!");
        }
        lessonTimesService.batchSaveForStudent(request);

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryLessonInfoForStudent")
    public AjaxResult queryLessonInfoForStudent(LessonTimesRequest bean){
        if(StringUtils.isEmpty(bean.getLessonId())){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误!");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonTimesService.queryLessonInfoForStudent(bean));
    }
    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        lessonTimesService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        lessonTimesService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 新增和保存
     * @param request 实体对象
     * @return
     */
    @PostMapping(value="/queryLessonAuditCalcu")
    @Transactional
    public AjaxResult queryLessonAuditCalcu(@Validated LessonTimesRequest request){
        if(StringUtils.isEmpty(request.getLessonId())){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误!");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", lessonTimesService.queryLessonAuditCalcu(request));
    }

    /**
     * 重新备课
     * @param request 实体对象
     * @return
     */
    @PostMapping(value="/reLesson")
    public AjaxResult reLesson(@Validated LessonTimesRequest request){
        if(null == request.getSourceType()){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误!");
        }
        lessonTimesService.reLesson(request);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 查询课程信息
     * @author 赵文博
     * @date 2021/6/21 13:52
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/queryLessonInfoContent")
    public AjaxResult queryLessonInfoContent(LessonTimesRequest bean){
        LessonTimesDto lessonTimesDto = lessonTimesService.queryLessonInfoContent(bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", lessonTimesDto);
    }

    /**
     * 查询课中练习试题
     * @author 赵文博
     * @date 2021/6/21 15:46
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     **/
    @GetMapping("/practiceQuestion")
    public AjaxResult practiceQuestion(LessonTimesRequest bean){
        List<QuestionDto> questionDtoList = lessonTimesService.getPracticeQuestion(bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"", questionDtoList);
    }
}

